package device_ctr;

import com.ouken.activity.AddDeviceInputPWDActivity;
import com.ouken.activity.R;

import android.app.AlertDialog;
import android.util.Log;
import android.widget.Toast;
public class Dev_logic {
	public boolean check_pw(String pass){
		int net_new_pass=update_pass();
		if (net_new_pass>0){
			Dev_comu kk=new Dev_comu("device1");
			kk.send_msg(":CN",net_new_pass);
			while (kk.isHasFinished()==false)
				;
			String result=kk.jie_mi(kk.getHtml_msg(),net_new_pass);
			if (result.startsWith("NP")){
				Dev_comu kk1=new Dev_comu("device1");
				kk1.send_msg(":GP" + pass+",",net_new_pass);
				while (kk1.isHasFinished()==false)
					;
				String result2=kk1.jie_mi(kk1.getHtml_msg(),net_new_pass);
				if (result2.startsWith("OK"))
					return true;
				else
					return false;
			}
			else
				return false;
		}
		else{
			return false;
		}
	}
	public String set_device(String pass,String Dev_Value){
		int net_new_pass=update_pass();
		if (net_new_pass>0){
			Dev_comu bb=new Dev_comu("device1");
			bb.send_msg(":SD" + pass + "," + Dev_Value,net_new_pass);
			while (bb.isHasFinished()==false)
				;
			String result=bb.jie_mi(bb.getHtml_msg(),net_new_pass);
			return result.toString();
		}
		else{
			return "ERROR,update pass error";
		}
	}
	public String read_device(String pass){
		int net_new_pass=update_pass();
		if (net_new_pass>0){
			Dev_comu kk=new Dev_comu("device1");
			kk.send_msg(":GT" + pass + ",",net_new_pass);
			while (kk.isHasFinished()==false)
				;
			String result=kk.jie_mi(kk.getHtml_msg(),net_new_pass);
			return result.toString();
		}
		else{
			return "ERROR,update pass error";
		}
	}
	
	public String change_device_pwd(String old_pass,String new_pass){
		int net_new_pass=update_pass();
		String change_pass_result;
		if (net_new_pass>0){
			Dev_comu kk=new Dev_comu("device1");
			kk.send_msg(":CP" + old_pass + "," + new_pass + ",",net_new_pass);
			while (kk.isHasFinished()==false)
				;
			String result=kk.jie_mi(kk.getHtml_msg(),net_new_pass);
			change_pass_result= result.toString();
			return change_pass_result;
		}
		else{
			return "ERROR,update pass error";
		}
	}
	public String change_device_pwd_try(String new_pass){
		int net_new_pass=update_pass();
		if (net_new_pass>0){
			Dev_comu kk=new Dev_comu("device1");
			kk.send_msg(":CP" + new_pass + "," + new_pass + ",",net_new_pass);
			while (kk.isHasFinished()==false)
				;
			String result=kk.jie_mi(kk.getHtml_msg(),net_new_pass);
			return result.toString();
		}
		else{
			return "ERROR,try to update pass error";
		}
	}
	public int update_pass(){
		Dev_comu kk=new Dev_comu("device1");
		kk.send_msg(":MP" + "0000" + "," + "01111111",0);
		while (kk.isHasFinished()==false)
			;
		String new_pass=kk.jie_mi(kk.getHtml_msg(),0);
		new_pass=new_pass.replace(" ", "");
		try {
			int new_pass_int=Integer.valueOf(new_pass);
			return new_pass_int;
		} catch (NumberFormatException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return 0;
		}
	}
}
